//
//  HomeWebsiteFlipItemView.m
//  BaoVn
//
//  Created by Le Anh Tai on 11/8/14.
//  Copyright (c) 2014 Le Anh Tai. All rights reserved.
//

#import "HomeWebsiteFlipItemView.h"
#import "ListNewsItemView.h"
#import "CardNewsItemView.h"

@interface HomeWebsiteFlipItemView()<LazyLoadingDelegate>

@end

@implementation HomeWebsiteFlipItemView

- (instancetype)init
{
    if (self = [super init])
    {
        _margin = 0;
        _padding = 0;
        
        _newsType = [AppInfo shared].newsType;
    }
    
    return self;
}

- (void)loadDataWithValue:(id)value
{
    _data = value;
    _countImageFailed = 0;
    
    [self initializeItemViews:_data.count];
    
    for (NSInteger i = 0; i < _data.count; i++)
    {
        BaseNewsItemView *itemView = [_itemViews objectAtIndex:i];
        [itemView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)]];
        NSDictionary *dic = [_data objectAtIndex:i];
        [itemView loadData:dic];
    }
}

- (void)initializeItemViews:(NSInteger)numberOfItems
{
    if (_itemViews == nil)
    {
        _itemViews = [[NSMutableArray alloc] init];
    }
    
    //reuse childViews
    if (_itemViews.count == numberOfItems)
    {
        return;
    }
    
    for (UIView *view in _itemViews)
    {
        [view removeFromSuperview];
    }
    
    [_itemViews removeAllObjects];
    
    for (NSInteger i = 0; i < numberOfItems; i++)
    {
        BaseNewsItemView *itemView;
        if (_newsType == NewsTypeItem)
        {
            itemView = [[ListNewsItemView alloc] init];
        }
        else
        {
            itemView = [[CardNewsItemView alloc] init];
        }
        
        itemView.delegateLazyLoading = self;

        [self addSubview:itemView];
        [_itemViews addObject:itemView];
    }
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    NSInteger top = _margin;
    NSInteger hei;
    NSInteger commonPadding = 8;
    
    if (_newsType == NewsTypeItem)
    {
        hei = (self.height - _margin*2 - (_itemViews.count - 1)*_padding - commonPadding)/_itemViews.count;
    }
    else
    {
        hei  = (self.height - _margin*2 - (_itemViews.count - 1)*_padding)/_itemViews.count;
        _padding = commonPadding;
    }
    
    for (NSInteger i = 0; i < _data.count; i++)
    {
        BaseNewsItemView *itemView = [_itemViews objectAtIndex:i];
        itemView.frame = RECT(_margin, top, self.width - _margin*2, hei);
        
        top = itemView.bottom + _padding;
    }
}

#pragma mark -
- (void)tapGestureHandler:(UITapGestureRecognizer *)gesture
{
    BaseNewsItemView *view = (BaseNewsItemView *)gesture.view;
    
    if ([self.delegate respondsToSelector:@selector(flipViewItem:didSelectItem:withData:)])
    {
        [self.delegate flipViewItem:self didSelectItem:view withData:view.data];
    }
}

#pragma mark -
- (void)newsItemView:(BaseNewsItemView *)view didLoadImageWithError:(NSString *)error
{
    if (error.length > 0)
    {
        _countImageFailed++;
        
        //hide image because all images load failed
        if (_countImageFailed == _itemViews.count)
        {
            for (BaseNewsItemView *itemView in _itemViews)
            {
                [itemView hideImage];
            }
        }
    }
}

@end
